<template>
  <div class="login-page">
    <div class="login-container">
      <!-- 左侧登录表单 -->
      <div class="login-form-container">
        <div class="login-header">
          <div class="logo-container" @click="$router.push('/')">
            <div class="logo-bg">
              <el-icon><QuestionFilled /></el-icon>
            </div>
            <h1 class="site-name">智问社区</h1>
          </div>
          <h2 class="login-title">用户登录</h2>
          <p class="login-subtitle">欢迎回来，请登录您的账号</p>
        </div>

        <!-- 登录表单 -->
        <el-form
          ref="loginFormRef"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
          @submit.prevent="handleLogin"
        >
          <!-- 邮箱/用户名 -->
          <el-form-item prop="account">
            <el-input
              v-model="loginForm.account"
              placeholder="邮箱/用户名"
            >
              <template #prefix>
                <el-icon><User /></el-icon>
              </template>
            </el-input>
          </el-form-item>

          <!-- 密码 -->
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="密码"
              show-password
            >
              <template #prefix>
                <el-icon><Lock /></el-icon>
              </template>
            </el-input>
          </el-form-item>

          <!-- 记住我和忘记密码 -->
          <div class="form-options">
            <el-checkbox v-model="loginForm.remember">记住我</el-checkbox>
            <router-link to="/forgot-password" class="forgot-link">忘记密码?</router-link>
          </div>

          <!-- 登录按钮 -->
          <el-button
            type="primary"
            class="login-button"
            :loading="loading"
            @click="handleLogin"
            >登录</el-button>

          <!-- 第三方登录 -->
          <div class="third-party-login">
            <div class="divider">
              <span>或使用以下方式登录</span>
            </div>
            <div class="third-party-buttons">
              <el-button class="social-button">
                <el-icon><Promotion /></el-icon>
              </el-button>
              <el-button class="social-button">
                <el-icon><User /></el-icon>
              </el-button>
              <el-button class="social-button">
                <el-icon><Platform /></el-icon>
              </el-button>
            </div>
          </div>

          <!-- 注册链接 -->
          <div class="register-link-container">
            还没有账号? <router-link to="/register" class="register-link">立即注册</router-link>
          </div>
        </el-form>
      </div>

      <!-- 右侧背景 -->
      <div class="login-background">
        <div class="background-content">
          <h2 class="background-title">智问社区</h2>
          <p class="background-text">专业问答交流平台</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import { useUserStore } from '../stores/user';
import { 
  User, 
  Lock, 
  QuestionFilled, 
  Promotion,
  Platform
} from '@element-plus/icons-vue';

const router = useRouter();
const userStore = useUserStore();
const loading = ref(false);
const loginFormRef = ref(null);
const loginForm = ref({
  account: '',
  password: '',
  remember: false
});

const loginRules = {
  account: [
    { required: true, message: '请输入邮箱/用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度至少为6个字符', trigger: 'blur' }
  ]
};

const handleLogin = async () => {
  if (loginFormRef.value) {
    try {
      await loginFormRef.value.validate();
      loading.value = true;
      
      // 调用登录接口
      await userStore.loginAction({
        account: loginForm.value.account,
        password: loginForm.value.password
      });
      
      ElMessage.success('登录成功');
      
      // 根据用户角色决定跳转页面
      // 管理员用户跳转到管理后台
      if (userStore.userInfo?.role === 3) {
        router.push('/admin');
      } else {
        // 非管理员用户跳转到首页
        router.push('/');
      }
    } catch (error) {
      console.error('登录失败:', error);
      ElMessage.error(error.message || '登录失败，请重试');
    } finally {
      loading.value = false;
    }
  }
};
</script>

<style scoped>
.login-page {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f5f7fa;
}

.login-container {
  display: flex;
  width: 900px;
  height: 600px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  border-radius: 12px;
  overflow: hidden;
}

.login-form-container {
  flex: 1;
  background-color: #fff;
  padding: 40px;
  display: flex;
  flex-direction: column;
}

.login-header {
  margin-bottom: 40px;
}

.logo-container {
  display: flex;
  align-items: center;
  margin-bottom: 24px;
  cursor: pointer;
}

.logo-bg {
  width: 40px;
  height: 40px;
  border-radius: 8px;
  background: linear-gradient(135deg, #1e3a8a 0%, #2563eb 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 20px;
}

.site-name {
  margin: 0 0 0 10px;
  font-size: 18px;
  font-weight: bold;
  color: #333;
}

.login-title {
  font-size: 24px;
  font-weight: 600;
  color: #333;
  margin: 0 0 8px;
}

.login-subtitle {
  color: #666;
  margin: 0;
}

.login-form {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.forgot-link {
  color: #409eff;
  text-decoration: none;
  font-size: 14px;
}

.login-button {
  width: 100%;
  height: 44px;
  font-size: 16px;
  margin-bottom: 24px;
}

.third-party-login {
  margin-bottom: 24px;
}

.divider {
  display: flex;
  align-items: center;
  margin: 20px 0;
  color: #999;
  font-size: 14px;
}

.divider::before,
.divider::after {
  content: '';
  flex: 1;
  height: 1px;
  background-color: #eee;
  margin: 0 16px;
}

.third-party-buttons {
  display: flex;
  justify-content: center;
  gap: 16px;
}

.social-button {
  width: 40px;
  height: 40px;
  padding: 0;
  border-radius: 50%;
  border: 1px solid #eee;
}

.register-link-container {
  text-align: center;
  color: #666;
  font-size: 14px;
}

.register-link {
  color: #409eff;
  text-decoration: none;
  font-weight: 500;
}

.login-background {
  width: 40%;
  background: linear-gradient(135deg, #1e3a8a 0%, #2563eb 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  text-align: center;
  padding: 40px;
}

.background-title {
  font-size: 32px;
  font-weight: 700;
  margin-bottom: 16px;
}

.background-text {
  font-size: 18px;
  opacity: 0.9;
}

/* 响应式调整 */
@media (max-width: 768px) {
  .login-container {
    width: 100%;
    height: 100vh;
    border-radius: 0;
  }
  
  .login-background {
    display: none;
  }
}
</style> 